﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using System.Data.Entity;
using PagedList;
using PagedList.Mvc;
using WineStore.Models;


namespace WineStore.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Index(Int32? categoryId, string currentFilter, string searchString, string currentFilter2, string searchString2, string currentFilter3, string searchString3, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
                searchString2 = currentFilter2;
                searchString3 = currentFilter3;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;
            ViewBag.CurrentFilter2 = searchString2;
            ViewBag.CurrentFilter3 = searchString3;
            using (var db = new WineDbContext())
            {
                var viewModel = new ViewIndexData();
                // Lay ve list category va list product theo categoryId
                viewModel.Categories = (db.Categories
                        .Include(c => c.Products)
                        .OrderByDescending(c => c.CategoryId)).ToList();

                // Lay ve list product
                viewModel.Products = db.Products
                                     .OrderByDescending(p=>p.ProductId);

                if (!String.IsNullOrEmpty(searchString))
                {
                    viewModel.Products = viewModel.Products.Where(p => p.ProductName.ToUpper().Contains(searchString.ToUpper()));
                }
                if (!String.IsNullOrEmpty(searchString2))
                {
                    viewModel.Products = viewModel.Products.Where(p => ((int)p.ProductPrice)>=Int32.Parse(searchString2));
                }
                if (!String.IsNullOrEmpty(searchString3))
                {
                    viewModel.Products = viewModel.Products.Where(p => ((int)p.ProductPrice) <= Int32.Parse(searchString3));
                }

                // Lay ve list product theo categoryId
                if (categoryId != null)
                {
                    ViewBag.CategoryId = categoryId.Value;
                    viewModel.Products = viewModel.Categories.Where(c => c.CategoryId == categoryId.Value).Single().Products;
                }

                int pageSize = 3;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;
                ViewBag.Pro = pro;
                
                // Hien thi Category
                ViewBag.Categories = viewModel.Categories;

                var cart = ShoppingCart.GetCart(this.HttpContext);
                ViewBag.TotalCartItems = cart.TotalCartItems();

                // Hien thi Product
                return View(viewModel.Products.ToPagedList(pageNumber, pageSize));
            }
        }

        public ViewResult IndexPartial(Int32? categoryId, string currentFilter, string searchString, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;
            using (var db = new WineDbContext())
            {
                var viewModel = new ViewIndexData();
                // Lay ve list category va list product theo categoryId
                viewModel.Categories = (db.Categories
                        .Include(c => c.Products)
                        .OrderByDescending(c => c.CategoryId)).ToList();

                // Lay ve list product
                viewModel.Products = db.Products
                                     .OrderByDescending(p => p.ProductId);

                if (!String.IsNullOrEmpty(searchString))
                {
                    viewModel.Products = viewModel.Products.Where(p => p.ProductName.ToUpper().Contains(searchString.ToUpper()));
                }

                // Lay ve list product theo categoryId
                if (categoryId != null)
                {
                    ViewBag.CategoryId = categoryId.Value;
                    viewModel.Products = viewModel.Categories.Where(c => c.CategoryId == categoryId.Value).Single().Products;
                }

                int pageSize = 3;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;
                ViewBag.Pro = pro;

                // Hien thi Category
                ViewBag.Categories = viewModel.Categories;

                var cart = ShoppingCart.GetCart(this.HttpContext);
                ViewBag.TotalCartItems = cart.TotalCartItems();

                // Hien thi Product
                return View(viewModel.Products.ToPagedList(pageNumber, pageSize));
            }
        }


        // Autocomplete for textbox search 
        [HttpGet]
        public ActionResult Autocomplete(string term)
        {
            using (var db = new WineDbContext())
            {
                var productNames = from p in db.Products
                                   select p.ProductName;
                string[] items = productNames.ToArray();

                var filteredItems = items.Where(
                    item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
                    );
                return Json(filteredItems, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
